package com.qipay.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Created by back on 2016/9/15.
 */
@Controller
public class BaseController {
    @Value("${web.static.url.host}")
    protected String StaticHost;

    protected ModelAndView createModelAndView(String name){
        ModelAndView modelAndView = new ModelAndView(name);
        modelAndView.addObject("StaticHost", StaticHost);
        return modelAndView;
    }

    public String generateAutoSubmitHtml(Map<String, Object> paramMap, String actionUrl) {

        StringBuilder html = new StringBuilder();
        html.append("<html><head></head><body>").append("<form id='pay_form' name='pay_form' action='")
                .append(actionUrl).append("' method='POST'>\n");

        for (String key : paramMap.keySet()) {
            String value = paramMap.get(key).toString();
            //银联快捷，输入验证码输入框的不隐藏
            if("verifyCode".equals(key))html.append("<input type='text' name='" + key + "' value='" + value + "'>\n");
            if( value!= null && !value.isEmpty()){
                html.append("<input type='hidden' name='" + key + "' value='" + value + "'>\n");

            }
        }

        html.append("</form>\n")
                .append("<script language='javascript'>window.onload=function(){document.pay_form.submit();}</script>\n")
                .append("</body></html>");

        return html.toString();
    }
}
